<?php
/**
 * Created by PhpStorm.
 * Author: sitewang@aliyun.com
 * CreateTime: 2023/11/6 22:36
 * Blog：www.myblogs.xyz
 */

namespace app\infrastructure\model;

trait QueryTrait
{
	/**
	 * 获取单个字段数据
	 * @param string|array $where
	 * @param string|array $field
	 * @param string $order
	 * @param string|array $whereOr
	 * @return string
	 */
	public function getField($where = '', $field = '', string $order = '', $whereOr = ''): string
	{
		if (empty($where) && empty($whereOr)) return '';
		if (empty($field)) return '';
		$value = $this->query($where, '', $order, $whereOr)->value($field);
		return empty($value) ? '' : $value;
	}

	/**
	 * 获取单条数据
	 * @param string|array $where
	 * @param string|array $field
	 * @param string $order
	 * @param string|array $whereOr
	 * @return array|null
	 */
	public function getFind($where = '', $field = '', string $order = '', $whereOr = ''): array
	{
		if (empty($where) && empty($whereOr)) return [];
		$query = $this->query($where, $field, $order, $whereOr);
		return $query->findOrEmpty()->toArray();
	}

	/**
	 * 获取列表数据
	 * @param string|array $where
	 * @param string|int $limit
	 * @param string $order
	 * @param string|array $field
	 * @param string|array $whereOr
	 * @return array
	 */
	public function getList($where = '', $limit = '', string $order = '', $field = '', $whereOr = ''): array
	{
		$query = $this->query($where, $field, $order, $whereOr);
		if (!empty($limit)){
			if (is_numeric($limit)){
				$query = $query->limit($limit);
			}else{
				$limit_length = explode(',', $limit);
				$limit = intval($limit_length[0]);
				$length = intval($limit_length[1]);;
				$query = $query->limit($limit, $length);
			}
		}
		$list = $query->select()->toArray();
		if (!empty($list)){
			foreach ($list as &$val){
				if (isset($val['create_time']) && is_numeric($val['create_time'])){
					$val['create_time'] = date('Y-m-d H:i', $val['create_time']);
				}
				if (isset($val['update_time']) && is_numeric($val['update_time'])){
					$val['update_time'] = date('Y-m-d H:i', $val['update_time']);
				}
			}
		}
		return $list;
	}

	/**
	 * 获取列表分页数据
	 * @param string|array $where
	 * @param string|int $limit
	 * @param string $order
	 * @param string|array $field
	 * @param string|array $whereOr
	 * @return array
	 */
	public function getListPage($where = '', $limit = 1, string $order = '', $field = '', $whereOr = ''): array
	{
		$query = $this->query($where, $field, $order, $whereOr);
		// 分页数据处理
		$count = $query->count();
		$list = $query->paginate($limit);
		$page = $list->render();
		$data = $list->toArray();
		if (!empty($data['data'])){
			if (is_array($data['data'])){
				foreach ($data['data'] as &$val){
					if (isset($val['create_time']) && is_numeric($val['create_time'])){
						$val['create_time'] = date('Y-m-d H:i', $val['create_time']);
					}
					if (isset($val['update_time']) && is_numeric($val['update_time'])){
						$val['update_time'] = date('Y-m-d H:i', $val['update_time']);
					}
				}
			}
		}
		return [
			'count'=>$count,
			'page'=>$page,
			'list'=>$data['data']
		];
	}

	/**
	 * 检测是否存在
	 * @param string|array $where
	 * @return bool
	 */
	public function getExists($where = ''): bool
	{
		return $this->countExists($where);
	}

	/**
	 * 数量统计
	 * @param string|array $where
	 * @return int
	 */
	public function getCount($where = ''): int
	{
		return $this->countExists($where, false);
	}

	private function countExists($where = '', $is_bool = true)
	{
		$wheres = empty($where) ? self::wheres() : $where;
		// 数据查询
		$query = $this->db();
		// 存在where查询
		if (!empty($wheres)){
			// 主键查询
			if (is_numeric($wheres)){
				$query = $query->where($this->pk, $wheres);
			}else{
				$query = $query->where($wheres);
			}
		}
		$rows = $query->count();
		if ($is_bool){
			if (empty($rows)){
				return false;
			}
			return true;
		}
		return $rows;
	}

	/**
	 * @param string|array $where
	 * @param string $field
	 * @return float
	 */
	public function getSum($where = '', string $field = '')
	{
		$wheres = empty($where) ? self::wheres() : $where;
		if (empty($field)) return 0;
		// 数据查询
		$query = $this->db();
		// 存在where查询
		if (!empty($wheres)){
			// 主键查询
			if (is_numeric($wheres)){
				$query = $query->where($this->pk, $wheres);
			}else{
				$query = $query->where($wheres);
			}
		}
		return $query->sum($field);
	}
}